package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.ICompanyDao;
import com.cn.wanxi.dao.impl.CompanyImpl;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.service.ICompanyService;
import redis.clients.jedis.Jedis;

public class CompanyServiceImpl implements ICompanyService {
    @Override
    public CompanyModel getCompanyModel() {
        //定义一个空的对象，用来装从数据层查询出来的数据
        CompanyModel companyModel = new CompanyModel();
        ICompanyDao iCompanyDao = new CompanyImpl();//主要用来调用数据层的方法
//判断 缓存里面是否有公司信息，如果有，则取缓存里面的值，如果没有，则查询数据库
        Jedis jedis = new Jedis();//实例化对象
        boolean isHave = jedis.exists("id");
        if (isHave) {
            //缓存数据
            companyModel.setLogo(jedis.get("logo"));
            companyModel.setPhone(jedis.get("phone"));
            companyModel.setId(Integer.parseInt(jedis.get("id")));
//            System.out.println("数据来自于缓存");

        } else {
            companyModel = iCompanyDao.getCompanyModel();//通过调用方法，返回model数据
            jedis.setex("logo",30*60 ,companyModel.getLogo());
            jedis.set("phone", companyModel.getPhone());
            jedis.set("id", companyModel.getId().toString());
//            jedis.expire("id",30*60);
//            jedis.expire("logo",30*60);
//            System.out.println("数据来自于数据库");


        }

        return companyModel;
    }
}
